package com.lph.util;

import java.util.HashMap;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.mail.HtmlEmail;

import com.cloopen.rest.sdk.CCPRestSmsSDK;

/**
 * JAVA邮件的发送
 * @author Administrator
 *
 */
public class EmailUtil {
	public static boolean sendEmsil(String to,String com){
		
		boolean flat=false;
		try {
			//鍒涘缓缃戦〉閭瀵硅薄
			HtmlEmail email=new HtmlEmail();
			//鍩烘湰璁剧疆
			email.setSSL(true);
			email.setDebug(true);
			//璁剧疆qq閭涓哄彂閫佸湴鍧�
			email.setHostName("SMTP.qq.com");
			email.setSmtpPort(587);
		
			//璁剧疆鏈嶅姟鍣ㄩ獙璇�
			//email.setHostName("CardDAV/CalDAV.qq.com");
			//email.setAuthentication("2424826620@qq.com","oerdhniubquzeccg");//鎺堟潈鐮�
			email.setAuthentication("2424826620@qq.com","vmfqzlonqktidihf");//授权码
			//璁剧疆閭欢鍙戦�佷汉
			email.setFrom("2424826620@qq.com");
			//璁剧疆鍙戦�佹爣棰�
			email.setSubject("商会登录系统");
			
			//鎺ユ敹浜�
			email.addTo(to);
			//鍙戦�佸唴瀹�
			email.setMsg(com);
			
			/*//鎷撳睍闄勪欢鍙戦��
			EmailAttachment ea==new E();
			
			//娣诲姞鍙戦�侀檮浠剁殑鍦板潃锛堢粷瀵硅矾寰勶級
*/			
			
			
			
			
			//閭欢鐨勫彂閫�
			email.send();
			flat=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flat;
		
	}
	public static boolean sendSms(String to,HttpServletRequest req){
		//短信的发送
		boolean flat=false;
		try {
			//声明短信发送对象
			CCPRestSmsSDK restApi=new CCPRestSmsSDK();
			//设置参数
			restApi.init("app.cloopen.com", "8883");
			String sid="8aaf07086e0115bb016e4f70c1362c6f";
			String token="85fb252971fc483cb3386b38be8e9c0b";
			String appId="8aaf07086e0115bb016e4f70c18f2c75";
			restApi.setAccount(sid, token);
			restApi.setAppId(appId);
			
			Random r=new Random();
			String code=(r.nextInt(900000)+100000)+"";//短信验证码
			req.getSession().setAttribute("codePwd", to+"-"+code);
			HashMap<String, Object> result=restApi.sendTemplateSMS(to, "1", new String[]{code,"5"});//5分钟内有效
			System.out.println(result);
			//判断发送成功或失败
			if("000000".equals(result.get("statusCode"))){
				flat=true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flat;
	}
	
	public static void main(String[] args) {
		//sendEmsil("2424826620@qq.com", "我永远喜欢java");
		sendSms("18878629614",null);
	}
}
